package cn.duxiaod.demo.singleton;

/**
 * 双重校验实现单例模式
 */
public class SingletonClass {
    private volatile static SingletonClass singletonClass = null;

    public static SingletonClass getInstance() {
        if(singletonClass == null){
            synchronized (SingletonClass.class){
                if(singletonClass == null){
                    singletonClass = new SingletonClass();
                }
            }
        }
        return singletonClass;
    }

    //私有化构造方法
    private SingletonClass(){

    }

    public static void main(String[] args) {
        SingletonClass instance = SingletonClass.getInstance();
        SingletonClass instance2 = SingletonClass.getInstance();
        SingletonClass instance3 = SingletonClass.getInstance();
        System.out.println(instance == instance2);//true
        System.out.println(instance == instance3);//true
    }
}
